package com.youdianstar.app.common.widget.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.databinding.PagingFooterItemBinding


/**
 * File: FooterAdapter
 * Author: 82149 Create: 2021/9/22 18:33
 * Changes (from 2021/9/22)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class FooterBDAdapter (private val retry: () -> Unit) : LoadStateAdapter<FooterBDAdapter.FooterViewHolder>(){

    class FooterViewHolder(binding: PagingFooterItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var pagingBinding = binding
    }

    override fun onBindViewHolder(holder: FooterViewHolder, loadState: LoadState) {
        holder.pagingBinding.run {
            progressBar.isVisible = loadState is LoadState.Loading
            btRetry.isVisible = loadState is LoadState.Error
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): FooterViewHolder {
        val binding = PagingFooterItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        //加载失败时，点击重新请求
        binding.btRetry.setOnClickListener {
            retry()
        }
        return FooterViewHolder(binding)
    }
}